package com.jin.tinker.app;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Description:SharedPreferences工具类
 * 随便写的，测试初始化用的
 * Date：2021/3/8
 * Time:15:43
 * author:J
 */
public class SpUtils {
    private volatile static SpUtils mSpUtils;
    private SharedPreferences sp;

    private SpUtils() {
    }

    public static SpUtils getInstance() {
        if (mSpUtils == null) {
            synchronized (SpUtils.class) {
                if (mSpUtils == null) {
                    mSpUtils = new SpUtils();
                }
            }
        }
        return mSpUtils;
    }

    /**
     * 初始化sp
     */
    public void initSp(Context context) {
        sp = context.getSharedPreferences("SP_NAME", Context.MODE_PRIVATE);
    }

    public void putString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    public String getString(String key) {
        return sp.getString(key, "");
    }

    public String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    public void putInt(String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    public int getInt(String key) {
        return sp.getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    public void putBoolean(String key, boolean b) {
        sp.edit().putBoolean(key, b).apply();
    }

    public boolean getBoolean(String key) {
        return sp.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }
}
